package com.heima.article.listenter;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.heima.article.mapper.ArticleConfigMapper;
import com.heima.common.constants.WmNewsMessageConstants;
import com.heima.model.article.pojos.ApArticleConfig;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Map;

/**
 * 向上或者向下侦听器
 *
 * @author DiXiaoNie
 * @date 2022/12/28
 * @since 2022/12/28 17:21
 */
@Component
public class DownOrUpListener {

    @Resource
    private ArticleConfigMapper articleConfigMapper;

    @KafkaListener(topics = WmNewsMessageConstants.WM_NEWS_UP_OR_DOWN_TOPIC)
    public void getMessage(String message) {
        //转换参数
        Map map = JSON.parseObject(message, Map.class);
        Long articleId = (Long) map.get("articleId");
        Integer enable = (Integer) map.get("enable");
        //修改文章配置
        ApArticleConfig articleConfig = this.articleConfigMapper.selectOne(Wrappers.<ApArticleConfig>lambdaQuery().eq(ApArticleConfig::getArticleId, articleId));
        if (articleConfig == null) {
            return;
        }
        //enable=0 表示下架 1表示上架
        //isdown = true 表示下架 false表示上架
        articleConfig.setIsDown(enable == 0 ? Boolean.TRUE : Boolean.FALSE);
        this.articleConfigMapper.updateById(articleConfig);
    }
}
